<template>
  <div class="add_notice_view" v-if="addNoticeShow" @click.stop="">
    <div class="add_notice_head">
      <div>添加群公告</div>
    </div>
    <div class="add_notice_main">
      <!-- {{groupNoticeText.replace(/\u200B/g, "")}} -->
      <el-input type="textarea" :rows="10" placeholder="请输入内容" v-model="groupNoticeText" ref="contentInput"
        @keyup.native="keyArr">
      </el-input>
      <div class="add_n_file">
        <div class="file_change" v-show="!noticeFile">
          <span></span>附件添加<input type="file" id="file" @change="uploadFile" />
        </div>
        <div class="del_n_file" v-show="noticeFile">
          <span class="del_n_icon"></span>
          <span class="del_n_content">{{
            noticeFile ? noticeFile.fileName : ""
          }}</span>
          <span class="del_n_del" @click.stop.prevent="noticeFile = null">删除</span>
        </div>
      </div>
    </div>
    <div class="add_n_footer">
      <div class="nt_adopt" @click.stop.prevent="addPublicNotice">确定</div>
      <div class="nt_refuse" @click.stop.prevent="noticeRefuse">取消</div>
    </div>
  </div>
</template>
<script>
import { addPublicNotice_axios } from "@/api/home";
export default {
  props: ["addNoticeShow", "myInfo", "toInfo"],
  data() {
    return {
      noticeFile: null,
      groupNoticeText: "",
    };
  },
  methods: {
    keyArr(e) {
      if (e.keyCode == 13) {
        document.execCommand("insertHTML", false, "<br>&ZeroWidthSpace;");
      }
    },
    noticeRefuse() {
      this.$emit("update:addNoticeShow", false);
    },
    uploadFile(e) {
      let _this = this;
      let fileInfo = e.target.files[0];
      let filesize = fileInfo.size / 1024 / 1024 || 0;
      if (filesize > 500) {
        this.$Message.warning("发送文件大小不能大于500M");
        return;
      }
      let fileReader = new FileReader();
      fileReader.onload = function (w) {
        document.getElementById("file").value = "";
        let form = _this.$utilJs.get_2_data(
          fileInfo,
          _this.myInfo.id,
          fileInfo.name
        );
        _this.noticeFile = {
          form: form,
          fileName: fileInfo.name,
        };
      };
      // 2022/5/12 zc新增 重新赋值通知文件，避免上传传参错误
      this.noticeFile = _this.noticeFile;
      fileReader.readAsDataURL(fileInfo);
    },
    addPublicNotice() {
      if (this.groupNoticeText.trim() == "") {
        this.$Message.warning("内容不能为空");
        return;
      }
      // 2022/7/14 新增 对文字内容做零宽字符串替换避免入库出现问号
      this.groupNoticeText = this.groupNoticeText.replace(/\u200B/g, '');
      this.$emit("update:addNoticeShow", false);
      //   this.addNoticeShow = false;
      if (this.noticeFile && this.noticeFile != null) {
        this.$Message.success("附件上传中");
        let times = new Date().getTime();

        this.$upload(
          "api/v1/oim/file/upload.do?times=" + times,
          this.noticeFile.form,
          (info) => {
            if (info.result == 200) {
              addPublicNotice_axios({
                value: this.groupNoticeText,
                istop: 0,
                userid: this.myInfo.id,
                groupid: this.toInfo.id,
                attach: info.data.id,
                attachname: this.noticeFile.fileName,
                username: this.myInfo.name,
                attachsize: info.data.size,
              })
                .then((res) => {
                  if (res.result == 200) {
                    this.noticeFile = null;
                    this.groupNoticeText = "";
                    this.$Message.success(res, "添加公告成功");
                    this.$emit('updateNotice');
                  } else {
                    this.$Message.warning(res.msg);
                  }
                })
                .catch((error) => {
                  this.$Message.warning("服务器出错,稍后再试15");
                });
            } else {
              this.$Message.warning(info.msg);
            }
          },
          (err) => {
            this.$Message.warning("服务器出错,稍后再试16");
          }
        );
      } else {
        addPublicNotice_axios({
          value: this.groupNoticeText,
          istop: 0,
          userid: this.myInfo.id,
          groupid: this.toInfo.id,
          attach: "",
          attachname: "",
          username: this.myInfo.name,
          attachsize: 0,
        })
          .then((res) => {
            if (res.result == 200) {
              this.noticeFile = null;
              this.groupNoticeText = "";
              this.$Message.success("添加公告成功");
              this.$emit('updateNotice');
            } else {
              this.$Message.warning(res.msg);
            }
          })
          .catch((error) => {
            this.$Message.warning("服务器出错,稍后再试17");
          });
      }
    },
  },
};
</script>
<style scoped>
</style>
